package View;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.media.control.*;
import javax.media.rtp.RTPControl;
import javax.media.rtp.ReceptionStats;
import javax.media.*; 
import javax.media.control.*; 
import javax.media.format.*; 

import Controller.Tasker;
import Date.ArrangeView;
import Events.GenericEvent;


public class ContentView implements ActionListener
{
	
	private JLabel label_content, label_menu,label_header,date,label_calendar,header;
	private int category;
	private boolean nuevo;
	private Vector<JButton> arrange;
	private Vector<JButton> view;
	private AddFrame fr;
	private Vector<JButton> options = new Vector<JButton>();
	private int arrangeSelection;
	private boolean[] status_view;
	private JFrame fram;
    private String dir = "images/";
    private Color azul = new Color(20,174,223);
    private Color gris = new Color(245,245,245);
    private JScrollPane Scroll, scroll2;
    private EventManager evman = new EventManager(); 
    private String months[] = new String[12];
    private JLabel backgroundLabel;
    private String dayWeek[] = new String[7];
    private int settingSelection=0;
    private JTextField input;
    private int statusRecurrence[] = new int[4];
    private Vector<JButton> recurrence = new Vector<JButton>();
    private int recurrenceselection=0;
    private String recurrenceTxt[] = new String[4];
    private JTextField numMinutes;
    private JLabel videolb;
    private Player mediaPlayer;
    private Component video;
    private String events[] = new String[3];
    private Tasker task;
    private JButton auto;
    private boolean stadoauto;
    private GenericEvent actualEvent;
    private ArrayList<JLabel> lb_posiciones;
    private ArrayList<ArrayList<JLabel>> lb_posiciones2;
    private ArrayList<GenericEvent> actual_events;
    private ArrayList<ArrayList<JButton>> bt_events;
    private ArrayList<ArrayList<ArrayList<ArrayList<ArrayList<GenericEvent>>>>> events_arrange;
    private ArrayList<ArrayList<ArrayList<ArrayList<JButton>>>> buttons_arrange;
    private GenericFrame genframe;
    private int indexsettings=0;
    private ArrangeView arrangeview;

	public ContentView(ArrangeView arrangeview,Tasker task,JLabel backgroundLabel,JLabel label_menu,JLabel label_content,ArrayList<GenericEvent> days, GenericFrame genframe) 
	{
		this.arrangeview=arrangeview;
		this.label_content=label_content;
		this.backgroundLabel = backgroundLabel;
		this.label_menu=label_menu;
		lb_posiciones = new ArrayList<JLabel>();
		this.actual_events = days;
		this.task = task;
		this.genframe=genframe;
		
		
		bt_events = new ArrayList<ArrayList<JButton>>();
		lb_posiciones2= new ArrayList<ArrayList<JLabel>>();
		events_arrange = new ArrayList<ArrayList<ArrayList<ArrayList<ArrayList<GenericEvent>>>>>();
		buttons_arrange = new ArrayList<ArrayList<ArrayList<ArrayList<JButton>>>>();
		stadoauto=task.getAutoStart();
		recurrenceselection=task.getRecurrentOption();
		
			  
		
		months[0]="January";
		months[1]="February";
		months[2]="March";
		months[3]="April";
		months[4]="May";
		months[5]="June";
		months[6]="July";
		months[7]="August";
		months[8]="September";
		months[9]="October";
		months[10]="November";
		months[11]="December";
		
		dayWeek[0]="Sunday";
		dayWeek[1]="Monday";
		dayWeek[2]="Tuesday";
		dayWeek[3]="Wednesday";
		dayWeek[4]="Thursday";
		dayWeek[5]="Friday";
		dayWeek[6]="Saturday";	
		
		recurrenceTxt[0]="Everyday";
		recurrenceTxt[1]="Every Hour";
		recurrenceTxt[2]="Each Week";
		recurrenceTxt[3]="Minutes";
		
		events[0]="Appointment";
		events[1]="Alarm";
		events[2]="Task";
		
		
		arrangeSelection=0;
		status_view = new boolean[2];
		status_view[0] = true;
		status_view[1] = true;
		
		
	}
	
	public void refreshView(int category)
	{
		this.category=category;
		
		if(category!=4)
			createMenuA();
		else
			createMenuB();
		
		
		
	}
	
	public void createMenuA()
	{
		label_menu.removeAll();
		
		arrange = new Vector<JButton>();
		view = new Vector<JButton>();
		
		JMenuBar label_edit = new JMenuBar();
		label_edit.setPreferredSize(new Dimension(250,55));
		label_edit.setLayout(new FlowLayout(FlowLayout.CENTER,20,0));
		label_edit.setBackground(gris);
		label_edit.setBorderPainted(false);
		label_menu.add(label_edit);
		
		JMenu bt = new JMenu();
		bt.setPreferredSize(new Dimension(100,45));
		bt.setIcon(new ImageIcon(dir+"E_"+0+".png"));
		bt.setContentAreaFilled(false);
		bt.setFocusPainted(false);
		bt.setBorderPainted(false);
			
		label_edit.add(bt);
		
		
		
		for(int i=0;i<3;i++)
		{
			JMenuItem menuItem = new JMenuItem();
			menuItem.setPreferredSize(new Dimension(200,30));
			menuItem.setBackground(Color.white);
			
			if(i==0)
				menuItem.setText("Create new Appointment");
			else if(i==1)
				menuItem.setText("Create new Alarm");
			else
				menuItem.setText("Create new Task");
			
			menuItem.setFont(new Font("Corbel", Font.BOLD, 16));
			menuItem.setForeground(azul);
			menuItem.addActionListener(this);
			menuItem.setActionCommand("ADD_"+i);
			
			bt.add(menuItem);
			
		}
		
		
		JButton bt2 = new JButton();
		bt2.setPreferredSize(new Dimension(100,45));
		bt2.addActionListener(this);
		bt2.setActionCommand("EDIT_"+1);
		bt2.setIcon(new ImageIcon(dir+"E_"+1+".png"));
		bt2.setContentAreaFilled(false);
		bt2.setFocusPainted(false);
		bt2.setBorderPainted(false);
			
		label_edit.add(bt2);
		
		
		
		JLabel label_arrange = new JLabel();
		label_arrange.setPreferredSize(new Dimension(400,70));
		label_arrange.setLayout(new FlowLayout(FlowLayout.CENTER,25,5));
		label_menu.add(label_arrange);
		
		for(int i2=0; i2<3 ; i2++)
		{
			JButton bt1 = new JButton();
			bt1.setPreferredSize(new Dimension(90,45));
			bt1.addActionListener(this);
			bt1.setActionCommand("ARRANGE_"+i2);
			
			if(arrangeSelection==i2)
				bt1.setIcon(new ImageIcon(dir+"A_"+i2+"_1.png"));
			else
				bt1.setIcon(new ImageIcon(dir+"A_"+i2+"_0.png"));
			bt1.setContentAreaFilled(false);
			bt1.setFocusPainted(false);
			bt1.setBorderPainted(false);
			arrange.add(bt1);
			label_arrange.add(arrange.elementAt(i2));
		}
		
		JLabel label_view = new JLabel();
		label_view.setPreferredSize(new Dimension(200,70));
		label_view.setLayout(new FlowLayout(FlowLayout.CENTER,20,2));
		label_menu.add(label_view);
		
		for(int i3=0; i3<2 ; i3++)
		{
			JButton bt1 = new JButton();
			
			if(status_view[i3])
				bt1.setIcon(new ImageIcon(dir+"select.png"));
			else
				bt1.setIcon(new ImageIcon(dir+"noselect.png"));
			
			bt1.setPreferredSize(new Dimension(20,20));
			bt1.setContentAreaFilled(false);
			bt1.setBorderPainted(false);
			bt1.addActionListener(this);
			bt1.setActionCommand("MENU_"+i3);
			String lb_txt="Tasker Events";
			String lb_txt2="Moodle Events";
				
			JLabel text = new JLabel();
			if(i3==0)
				text.setText(lb_txt);
			else
				text.setText(lb_txt2);
			
			text.setFont(new Font("Corbel", Font.BOLD, 18));
			text.setPreferredSize(new Dimension(120,25));
 			
			view.add(bt1);
			label_view.add(view.elementAt(i3));
			label_view.add(text);
		}
		
		label_menu.updateUI();
		refreshContent(0);
	}
	
	public void createMenuB()
	{
		label_menu.removeAll();
		options = new Vector<JButton>();
		
		JLabel temp = new JLabel();
		temp.setPreferredSize(new Dimension(990,70));
		temp.setLayout(new FlowLayout(FlowLayout.CENTER,5,0));
		
		for(int i=0; i<4 ; i++)
		{
			JButton bt1 = new JButton();
			
			if(settingSelection!=i)
				bt1.setIcon(new ImageIcon(dir+"S_"+i+"_0.png"));
			else
				bt1.setIcon(new ImageIcon(dir+"S_"+i+"_1.png"));
			
			bt1.setPreferredSize(new Dimension(240,60));
			bt1.setContentAreaFilled(false);
			bt1.setBorderPainted(false);
			bt1.setFocusPainted(false);
			bt1.addActionListener(this);
			bt1.setActionCommand("S_"+i);
			
			options.add(bt1);
			temp.add(options.elementAt(i));
		}
		
		label_menu.add(temp);
		label_menu.updateUI();
		refreshContent(0);
	}
	
	public void refreshContent(int action)
	{
	  if(category!=4)
	  {
		createGenericCalendarView(); 
		backgroundLabel.setIcon(new ImageIcon(dir+"background.png"));
		
		if(action==0)
			createDayView();
		else if(action==1)
			createWeekView();
		else
			createMonthView();
		
		//task.changeView(0);
		//metodo que se mandara llamar desde tasker
		//loadEvents(actual_events);
		backgroundLabel.updateUI();
	  }
	  else
	  {
		  loadOptionView(settingSelection);
	  }
	
	}
	
	public void updateMenuA()
	{
		for(int i=0; i<arrange.size();i++)
		{
			if(i==arrangeSelection)
			{
				arrange.elementAt(i).setIcon(new ImageIcon(dir+"A_"+i+"_1.png"));
			}
			else
				arrange.elementAt(i).setIcon(new ImageIcon(dir+"A_"+i+"_0.png"));	
			arrange.elementAt(i).updateUI();
		}
		
		for(int i2=0; i2<view.size();i2++)
		{
			if(status_view[i2])
			{
				view.elementAt(i2).setIcon(new ImageIcon(dir+"select.png"));
			}
			else 
				view.elementAt(i2).setIcon(new ImageIcon(dir+"noselect.png"));
			view.elementAt(i2).updateUI();
		}
		
	}
	
	public void updateMenuB()
	{
		for(int i2=0; i2<options.size();i2++)
		{
			if(settingSelection==i2)
			{
				options.elementAt(i2).setIcon(new ImageIcon(dir+"S_"+i2+"_1.png"));
			}
			else 
				options.elementAt(i2).setIcon(new ImageIcon(dir+"S_"+i2+"_0.png"));
			options.elementAt(i2).updateUI();
		}
	}
	
	public void createFormFrameB(int i, JLabel lab1, JLabel lab2, JLabel lab3)
	{
		indexsettings=i;
		JButton save = new JButton();
		save.setContentAreaFilled(false);
		save.setPreferredSize(new Dimension(150,60));
		save.addActionListener(this);
		save.setIcon(new ImageIcon(dir+"save.png"));
		save.setActionCommand("SAVE2");
		
		JButton cancel = new JButton();
		cancel.setPreferredSize(new Dimension(150,60));
		cancel.setContentAreaFilled(false);
		cancel.setIcon(new ImageIcon(dir+"cancel.png"));
		cancel.addActionListener(this);
		cancel.setActionCommand("CANCEL");
		
		if(i!=3 && i!=2)
		{
			lab3.add(cancel);
			lab3.add(save);
			lab3.updateUI();
		}
		
		if(i==0)
		{
			JLabel step = new JLabel();
			step.setPreferredSize(new Dimension(950,240));
			step.setIcon(new ImageIcon(dir+"step.png"));
			lab1.add(step);
			lab1.updateUI();
			
			JLabel contenturl = new JLabel();
			contenturl.setPreferredSize(new Dimension(650,120));
			contenturl.setLayout(new FlowLayout(FlowLayout.CENTER,0,20));
			
			JLabel txt = new JLabel();
			txt.setPreferredSize(new Dimension(650,30));
			txt.setFont(new Font("Corbel", Font.PLAIN, 20));
			txt.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
			txt.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			txt.setText("URL :"); 
			
			input = new JTextField();
			input.setPreferredSize(new Dimension(650,40));
			input.setFont(new Font("Corbel", Font.PLAIN, 20));
			input.setBackground(Color.white);
			
			
			contenturl.add(txt);
			contenturl.add(input);
			lab2.add(contenturl);
			lab2.updateUI();
			
		}
		else if(i==1)
		{
			JLabel refresh = new JLabel();
			refresh.setPreferredSize(new Dimension(900,60));
			refresh.setLayout(new FlowLayout(FlowLayout.RIGHT,20,10));
			
			JButton refreshbt = new JButton();
			refreshbt.setPreferredSize(new Dimension(80,50));
			refreshbt.addActionListener(this);
			refreshbt.setActionCommand("REFRESH");
			
			JLabel recurrencelb = new JLabel();
			recurrencelb.setPreferredSize(new Dimension(650,180));
			recurrencelb.setLayout(new FlowLayout(FlowLayout.CENTER,20,40));
			recurrence.removeAllElements();
			
			for(int k=0; k<4;k++)
			{
				JButton status = new JButton();
				status.setPreferredSize(new Dimension(20,20));
				status.addActionListener(this);
				status.setActionCommand("R_"+k);
				recurrence.add(status);				
				
				if(recurrenceselection!=k)
					recurrence.elementAt(k).setIcon(new ImageIcon(dir+"noselect.png"));
				else
					recurrence.elementAt(k).setIcon(new ImageIcon(dir+"select.png"));
				
				recurrencelb.add(recurrence.elementAt(k));
				
				JLabel txt = new JLabel();
				txt.setPreferredSize(new Dimension(250,30));
				txt.setFont(new Font("Corbel", Font.PLAIN, 20));
				txt.setText(recurrenceTxt[k]);
				
				JLabel opc4 = new JLabel();
				opc4.setPreferredSize(new Dimension(250,30));
				opc4.setLayout(new FlowLayout(FlowLayout.CENTER,5,0));
				
				numMinutes = new JTextField();
				numMinutes.setPreferredSize(new Dimension(60,30));
				numMinutes.setFont(new Font("Corbel", Font.PLAIN, 15));
				numMinutes.setBackground(Color.white);
				recurrencelb.add(txt);
				
				if(k==3)
				{
					opc4.add(numMinutes);
					txt.setPreferredSize(new Dimension(180,30));
					opc4.add(txt);
					recurrencelb.add(opc4);
				}			
				
			}
			
			JLabel lb = new JLabel();
			lb.setPreferredSize(new Dimension(600,80));
			lb.setLayout(new FlowLayout(FlowLayout.CENTER,10,40));
			
			auto = new JButton();
			auto.setPreferredSize(new Dimension(30,30));
			auto.addActionListener(this);
			auto.setActionCommand("AUTOSTART");
			auto.setContentAreaFilled(false);
			auto.setBorderPainted(false);
			if(stadoauto)
				auto.setIcon(new ImageIcon(dir+"select.png"));
			else
				auto.setIcon(new ImageIcon(dir+"noselect.png"));
			
			JLabel txt2 = new JLabel();
			txt2.setPreferredSize(new Dimension(500,30));
			txt2.setText("Auto Start (Start when windows starts)");
			txt2.setFont(new Font("Corbel", Font.PLAIN, 20));
					
			
			refresh.add(refreshbt);	
			lab1.add(refresh);
			lab1.add(recurrencelb);
			lb.add(auto);
			lb.add(txt2);
			lab2.add(lb);
			
			
			
		}
		else if(i==3)
		{
			videolb = new JLabel();
			videolb.setPreferredSize(new Dimension(690,210));
			lab1.add(videolb);
			loadVideo(dir+"help.mpg",videolb);			
			
			
		}
		
		
		
	}
	
	public void loadVideo(String direccion,JLabel content)
	{
		try
         {
         
         	File archivo = new File(direccion);
            URL mediaURL= new URL("file:/"+archivo.getAbsolutePath());           
			
			try
		      {
		      	if (mediaPlayer != null) 
	      		{
			        mediaPlayer.stop();
			        mediaPlayer.deallocate();
				}
				  mediaPlayer = Manager.createRealizedPlayer(mediaURL);
			      
			      ControllerListener cl = new ControllerAdapter()
			      {
			      	public void endOfMedia(EndOfMediaEvent eome) 
			      	{ 
			      		if (mediaPlayer != null) 
			      		{
			      			mediaPlayer.stop();
			      			mediaPlayer.deallocate();
			      			mediaPlayer.close();
			      			mediaPlayer= null;
						
			      		}
			      	} 
			      };
			      mediaPlayer.addControllerListener(cl);
			      video = mediaPlayer.getVisualComponent();
			      
			      content.setLayout(new FlowLayout(FlowLayout.CENTER));
			      if ( video != null ) 
		            content.add( video ); 
		            	
		          mediaPlayer.start();
		          content.updateUI();
		      } 
		      catch ( NoPlayerException noPlayerException )
		      {
		         System.err.println( "No media player found" );
		      } 
		      catch ( CannotRealizeException cannotRealizeException )
		      {
		         System.err.println( "Could not realize media player" );
		      }
		      catch ( IOException iOException )
		      {
		         System.err.println( "Error reading from the source" );
	      } 
			
      	
         } 
         catch ( MalformedURLException malformedURLException )
         {
            System.err.println( "Could not create URL for the file" );
         } 
         	
	}
	
	
	
	
	private void loadOptionView(int action) 
	{
		
		label_calendar.removeAll();
		backgroundLabel.setIcon(new ImageIcon(dir+"background2.png"));
		label_content.removeAll();
		
		JLabel info = new JLabel();
		info.setPreferredSize(new Dimension(950,450));
		info.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
		label_content.add(info); 
		
		JLabel lab1 = new JLabel();
		lab1.setPreferredSize(new Dimension(950,240));
		lab1.setLayout(new FlowLayout(FlowLayout.CENTER,0,20));
		
		JLabel lab2 = new JLabel();
		lab2.setPreferredSize(new Dimension(950,150));
		lab2.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
		
		JLabel lab3 = new JLabel();
		lab3.setPreferredSize(new Dimension(950,150));
		lab3.setLayout(new FlowLayout(FlowLayout.RIGHT,20,0));
		
		info.add(lab1);
		info.add(lab2);
		info.add(lab3);
		
		createFormFrameB(action,lab1,lab2, lab3);
		
		
		backgroundLabel.updateUI();
		label_content.updateUI();
		label_calendar.updateUI();
	}
	
	public void createGenericCalendarView()
	{
		label_content.removeAll();
		
		
		label_header = new JLabel();
		label_header.setPreferredSize(new Dimension(1000,40));
		label_header.setLayout(new FlowLayout(FlowLayout.CENTER,30,5));
		
		JButton previous = new JButton();
		previous.setPreferredSize(new Dimension(30,30));
		previous.addActionListener(this);
		previous.setActionCommand("PREVIOUS");		
		previous.setIcon(new ImageIcon(dir+"PREVIOUS.png"));
		previous.setContentAreaFilled(false);
		previous.setBorderPainted(false);
		previous.setFocusPainted(false);
		
		date = new JLabel();
		date.setPreferredSize(new Dimension(800,30));
		date.setFont(new Font("Corbel", Font.PLAIN, 25));
		date.setForeground(Color.gray);
		date.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		
		
		JButton next = new JButton();
		next.setPreferredSize(new Dimension(30,30));
		next.addActionListener(this);
		next.setActionCommand("NEXT");
		next.setIcon(new ImageIcon(dir+"NEXT.png"));
		next.setContentAreaFilled(false);
		next.setBorderPainted(false);
		next.setFocusPainted(false);
		
		
		label_header.add(previous);
		label_header.add(date);
		label_header.add(next);
				
		label_content.add(label_header);
		label_calendar = new JLabel();
		label_calendar.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
		label_calendar.setBackground(Color.white);
		label_calendar.setOpaque(true);
		
		JLabel label = new JLabel();
		label.setPreferredSize(new Dimension(930,30));
		label.setLayout(new FlowLayout(FlowLayout.CENTER));
		label.setIcon(new ImageIcon(dir+"label.png"));
		
		header= new JLabel();
		header.setPreferredSize(new Dimension(930,20));
		header.setForeground(Color.white);
		header.setFont(new Font("Corbel", Font.PLAIN, 17));
		
		Scroll = new JScrollPane();
		Scroll.setPreferredSize(new Dimension(920,370));
		Scroll.setBorder(BorderFactory.createLineBorder(Color.white));
		Scroll.getViewport().add(label_calendar);
		
		label.add(header);
		label_content.add(label);
		label_content.add(Scroll);
		label_content.updateUI();
	}
	
	public void actualizaHeader(String s)
	{
		header.setText(s);
		header.updateUI();
	}

	public void createDayView()
	{
		arrangeSelection=0;
		lb_posiciones.clear();
		updateMenuA();
		label_calendar.removeAll();
		int size= 24*70;
		label_calendar.setPreferredSize(new Dimension(900,size));
		
		header.setIcon(new ImageIcon(""));
		header.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		header.updateUI();
		
		for(int i=0; i<24;i++)
		{
			JLabel timeline = new JLabel();
			timeline.setPreferredSize(new Dimension(900,70));
			timeline.setBorder(BorderFactory.createLineBorder(azul));
			timeline.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
			
			JLabel time = new JLabel();
			time.setPreferredSize(new Dimension(100,60));
			time.setLayout(new FlowLayout(FlowLayout.RIGHT));
			time.setIcon(new ImageIcon(dir+"time.png"));
			
			JLabel hora = new JLabel();
			if(i==24)
				hora.setText("0:00");
			else
				hora.setText(i+":00");
			hora.setPreferredSize(new Dimension(70,50));
			hora.setFont(new Font("Corbel", Font.PLAIN, 30));
			hora.setForeground(Color.gray);
			time.add(hora);
			
			JLabel listado = new JLabel();
			listado.setPreferredSize(new Dimension(780,60));
			listado.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
			
			lb_posiciones.add(listado);
			
			timeline.add(time);
			timeline.add(listado);
			label_calendar.add(timeline);
		}
		
		

		Point NuevoPunto = new Point(0,490);	
		Scroll.getViewport().setViewPosition(NuevoPunto);
		Scroll.updateUI();
		label_calendar.updateUI();
	}
	
	
	public void loadEvents(ArrayList<GenericEvent> actualEvents2)
	{
		for(int in= 0; in<lb_posiciones.size();in++)
			lb_posiciones.get(in).removeAll();
		
		for(int h=0;h<lb_posiciones2.size();h++)
			for(int g=0;g<lb_posiciones2.get(h).size();g++)
				lb_posiciones2.get(h).get(g).removeAll();	
			
		actual_events = actualEvents2;
		
		
		
		// aqui ya debo tener los eventos bonitos y acomodados :P
	
		ArrangeEvents(actual_events);
		
		if(arrangeSelection==0)
		{
			int size = 0;
			if(actual_events.size()!=0)
				size=780/actual_events.size();
			
			for(int i=0; i<events_arrange.get(0).get(0).size();i++)
			{
				int sum = events_arrange.get(0).get(0).get(i).get(0).size() + events_arrange.get(0).get(0).get(i).get(1).size()+events_arrange.get(0).get(0).get(i).get(2).size();
				
				if(sum>6)
				{
					for(int j=0; j<events_arrange.get(0).get(0).size();j++)
					{
						for(int m=0; m<3;m++)
						{
							if(events_arrange.get(0).get(0).get(j).get(m).size()>0)
							{
								JLabel empty = new JLabel();
								empty.setPreferredSize(new Dimension(10,30));
								
								JLabel num = new JLabel(""+events_arrange.get(0).get(0).get(j).get(m).size());
								num.setPreferredSize(new Dimension(70,40));
								num.setForeground(Color.gray);
								num.setFont(new Font("Corbel", Font.PLAIN, 30));
								
								lb_posiciones.get(j).add(buttons_arrange.get(0).get(0).get(j).get(m));
								lb_posiciones.get(j).add(empty);
								lb_posiciones.get(j).add(num);
								lb_posiciones.get(j).updateUI();
							}
							
						}				
					}
				}					
				else
				{
						for(int j=0; j<events_arrange.get(0).get(0).get(i).size();j++)
						{
							ArrayList<JButton> bts = new ArrayList<JButton>();
						
							
							for(int m=0; m<events_arrange.get(0).get(0).get(i).get(j).size();m++)
							{
								JButton bt = new JButton();
								bt.setContentAreaFilled(false);
								bt.setBorderPainted(true);
								bt.setFocusPainted(false);
								bt.setLayout(new FlowLayout(FlowLayout.RIGHT,30,5));
								
								
								bt.setPreferredSize(new Dimension(size,60));
								bt.addActionListener(this);
								bt.setActionCommand("OPEN1_"+events_arrange.get(0).get(0).get(i).get(j).get(m).getIdEvent());
								
								JLabel lb = new JLabel();
								lb.setPreferredSize(new Dimension(size,60));
								lb.setLayout(new FlowLayout(FlowLayout.CENTER));
								
								JLabel icon = new JLabel();
								icon.setPreferredSize(new Dimension(40,40));
								if(events_arrange.get(0).get(0).get(i).get(j).get(m).getType()==0)
									icon.setIcon(new ImageIcon(dir+"icon0.png"));
								else if(events_arrange.get(0).get(0).get(i).get(j).get(m).getType()==1)
									icon.setIcon(new ImageIcon(dir+"icon1.png"));
								else
									icon.setIcon(new ImageIcon(dir+"icon2.png"));								
								
								lb.add(icon);
								
								JLabel txt = new JLabel("<html>"+events_arrange.get(0).get(0).get(i).get(j).get(m).getSubject()+"</html>");
								txt.setPreferredSize(new Dimension(70,30));
								txt.setFont(new Font("Corbel", Font.PLAIN, 12));
								lb.add(txt);
								bt.add(lb);
								lb_posiciones.get(events_arrange.get(0).get(0).get(i).get(j).get(m).getT_Start().getHours()).add(bt);
								lb_posiciones.get(events_arrange.get(0).get(0).get(i).get(j).get(m).getT_Start().getHours()).updateUI();
								bts.add(bt);
								bt_events.add(bts);
							}
						}
				}			
			}
		}
		else if(arrangeSelection==1)
		{
			
				
			
			
			for(int i=0; i<events_arrange.get(0).size();i++)
			{
				for(int j=0; j<events_arrange.get(0).get(i).size();j++)
					{
					
						for(int m=0; m<3;m++)
						{
							
							if(events_arrange.get(0).get(i).get(j).get(m).size()>0)
							{
								JLabel empty = new JLabel();
								empty.setPreferredSize(new Dimension(10,20));
								
								JLabel num = new JLabel(""+events_arrange.get(0).get(i).get(j).get(m).size());
								num.setPreferredSize(new Dimension(70,20));
								num.setForeground(Color.gray);
								num.setFont(new Font("Corbel", Font.PLAIN, 20));
								
								int dia = events_arrange.get(0).get(i).get(j).get(m).get(0).getD_Start().getDay();
								
															
								lb_posiciones2.get(events_arrange.get(0).get(i).get(j).get(m).get(0).getT_Start().getHours()).get(dia).add(buttons_arrange.get(0).get(i).get(j).get(m));
								lb_posiciones2.get(events_arrange.get(0).get(i).get(j).get(m).get(0).getT_Start().getHours()).get(dia).add(empty);
								lb_posiciones2.get(events_arrange.get(0).get(i).get(j).get(m).get(0).getT_Start().getHours()).get(dia).add(num);
								
							}
							
						}				
					}				
			}
		}
		else
		{
			
			for(int i=0; i<events_arrange.get(0).size();i++)
			{
				for(int j=0; j<events_arrange.get(0).get(i).size();j++)
					{
					
						for(int m=0; m<3;m++)
						{
							
							if(events_arrange.get(0).get(i).get(j).get(m).size()>0)
							{
								JLabel empty = new JLabel();
								empty.setPreferredSize(new Dimension(10,20));
								
								JLabel num = new JLabel(""+events_arrange.get(0).get(i).get(j).get(m).size());
								num.setPreferredSize(new Dimension(70,20));
								num.setForeground(Color.gray);
								num.setFont(new Font("Corbel", Font.PLAIN, 20));
								
								buttons_arrange.get(0).get(i).get(j).get(m).setPreferredSize(new Dimension(25,25));
								
								int dia = events_arrange.get(0).get(i).get(j).get(m).get(0).getD_Start().getDay();
								
															
								lb_posiciones2.get(0).get(dia).add(buttons_arrange.get(0).get(i).get(j).get(m));
								lb_posiciones2.get(0).get(dia).add(empty);
								lb_posiciones2.get(0).get(dia).add(num);
								
							}
							
						}				
					}				
			}
		}
		Scroll.updateUI();
		
	}

	public void ArrangeEvents(ArrayList<GenericEvent> actual_events)
	{
		
		events_arrange.clear();
		buttons_arrange.clear();
		
		for(int g=0; g<6; g++)
		{
			ArrayList<ArrayList<ArrayList<ArrayList<GenericEvent>>>> event_week = new ArrayList<ArrayList<ArrayList<ArrayList<GenericEvent>>>>();
			ArrayList<ArrayList<ArrayList<JButton>>> bt_week = new ArrayList<ArrayList<ArrayList<JButton>>>();
			
			for(int i=0; i<7;i++)
			{
				ArrayList<ArrayList<ArrayList<GenericEvent>>> event_day = new ArrayList<ArrayList<ArrayList<GenericEvent>>>();
				event_week.add(event_day);
				
				ArrayList<ArrayList<JButton>> bt_day = new ArrayList<ArrayList<JButton>>();
				bt_week.add(bt_day);
				
				for(int j=0; j<24;j++)
				{
					ArrayList<ArrayList<GenericEvent>> event_hour = new ArrayList<ArrayList<GenericEvent>>();
					event_day.add(event_hour);
					
					ArrayList<JButton> bt_hour = new ArrayList<JButton>();
					bt_day.add(bt_hour);
					
					for(int k=0; k<3;k++)
					{
						ArrayList<GenericEvent> event_catego = new ArrayList<GenericEvent>();
						event_hour.add(event_catego);
						
						JButton bt = new JButton(); 
						bt.setContentAreaFilled(false);
						bt.setBorderPainted(false);
						if(k==0)
							bt.setIcon(new ImageIcon(dir+"icon0.png"));
						else if(k==1)
							bt.setIcon(new ImageIcon(dir+"icon1.png"));
						else
							bt.setIcon(new ImageIcon(dir+"icon2.png"));
						
						if(arrangeSelection!=0)
							bt.setPreferredSize(new Dimension(15,15));
						else
							bt.setPreferredSize(new Dimension(40,40));
						bt.addActionListener(this);
						bt.setActionCommand("OPEN_"+g+"_"+i+"_"+j+"_"+k);
						bt_hour.add(bt);
												
					}
				}
			}
			buttons_arrange.add(bt_week);
			events_arrange.add(event_week);
		}
		
		
		if(arrangeSelection==0)
		{	
			
			for(int l=0; l<actual_events.size(); l++)
			{
				events_arrange.get(0).get(0).get(actual_events.get(l).getT_Start().getHours()).get(actual_events.get(l).getType()).add(actual_events.get(l));
			}
					
		}
		else if(arrangeSelection==1)
		{
			System.out.println(events_arrange);
			for(int l=0; l<actual_events.size(); l++)
			{
				events_arrange.get(0).get(actual_events.get(l).getD_Start().getDay()).get(actual_events.get(l).getT_Start().getHours()).get(actual_events.get(l).getType()).add(actual_events.get(l));
			}
		}
		else
		{

			for(int l=0; l<actual_events.size(); l++)
			{
				//falta ver lo de la semana a la que pertenece el dia con respecto al mes
				events_arrange.get(0).get(actual_events.get(l).getD_Start().getDay()).get(actual_events.get(l).getT_Start().getHours()).get(actual_events.get(l).getType()).add(actual_events.get(l));
			}
		}
		
		
	}
	
	public void actualizaLetrero(String s)
	{
		date.setText(s);
		date.updateUI();
	}

	public void createWeekView()
	{
		arrangeSelection=1;
		updateMenuA();
		header.setIcon(new ImageIcon(dir+"label2.png"));
		header.updateUI();
	
		
		label_calendar.removeAll();
		lb_posiciones2.clear();
	
		int size= 24*70;
		label_calendar.setPreferredSize(new Dimension(900,size));
		
		for(int i=0; i<24;i++)
		{
			ArrayList<JLabel> bt_week = new ArrayList<JLabel>();
						
			JLabel timeline = new JLabel();
			timeline.setPreferredSize(new Dimension(900,70));
			timeline.setBorder(BorderFactory.createLineBorder(azul));
			timeline.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
			
			JLabel time = new JLabel();
			time.setPreferredSize(new Dimension(100,60));
			time.setLayout(new FlowLayout(FlowLayout.RIGHT));
			time.setIcon(new ImageIcon(dir+"time.png"));
						
			
			
			JLabel hora = new JLabel();
			if(i==24)
				hora.setText("0:00");
			else
				hora.setText(i+":00");
			hora.setPreferredSize(new Dimension(70,50));
			hora.setFont(new Font("Corbel", Font.PLAIN, 30));
			hora.setForeground(Color.gray);
			time.add(hora);
			
			JLabel listado = new JLabel();
			listado.setPreferredSize(new Dimension(780,60));
			listado.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
			
			for(int k=0; k<7 ; k++)
			{
				JLabel event1 = new JLabel();
				event1.setPreferredSize(new Dimension(110,60));
				event1.setLayout(new FlowLayout(FlowLayout.RIGHT,0,0));
				event1.setBorder(BorderFactory.createLineBorder(azul));

				listado.add(event1);
				bt_week.add(event1);
				
			}	
					
			timeline.add(time);
			timeline.add(listado);
			label_calendar.add(timeline);
			lb_posiciones2.add(bt_week);
		}
		
		Point NuevoPunto = new Point(0,490);	
		Scroll.getViewport().setViewPosition(NuevoPunto);
		Scroll.updateUI();
		label_calendar.updateUI();
		
		
	}
	
	public void createMonthView()
	{
		arrangeSelection=2;
		updateMenuA();
		header.setIcon(new ImageIcon(dir+"label3.png"));
		header.updateUI();
		
		lb_posiciones2.clear();
		label_calendar.removeAll();
		
		int index=0;		
		int numberweeks = actual_events.size()/7;
		int size= numberweeks*115;
		label_calendar.setPreferredSize(new Dimension(900,size));
		
		
		
		for(int i=0; i<numberweeks;i++)
		{
			ArrayList<JLabel> bt_week = new ArrayList<JLabel>();
			JLabel timeline = new JLabel();
			timeline.setPreferredSize(new Dimension(900,110));
			timeline.setBorder(BorderFactory.createLineBorder(azul));
			timeline.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
			
			JLabel dayLabel = new JLabel();
			dayLabel.setPreferredSize(new Dimension(890,20));
			dayLabel.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
			timeline.add(dayLabel);
			
			
			for(int j=0; j<7;j++)
			{
				JLabel num = new JLabel();
				num.setPreferredSize(new Dimension(127,20));
				num.setText(""+actual_events.get(0).getD_Start().getDay());
				num.setFont(new Font("Corbel", Font.BOLD, 15));
				num.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
				dayLabel.add(num);
				index++;
			}
			
			Vector<JLabel> event = new Vector<JLabel>();
			for(int k=0; k<7 ; k++)
			{
				JLabel event1 = new JLabel();
				event1.setPreferredSize(new Dimension(122,90));
				event1.setLayout(new FlowLayout(FlowLayout.RIGHT,0,0));
				event1.setBorder(BorderFactory.createLineBorder(azul));

				event.add(event1);
				timeline.add(event1);
				bt_week.add(event1);
				
			}
			lb_posiciones2.add(bt_week);
					
			label_calendar.add(timeline);
		}
		label_calendar.updateUI();
		
		
	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		System.out.println(e.getActionCommand());
		
			
		if(e.getActionCommand().equals("NEXT"))
			task.changeTime(0);
		else if (e.getActionCommand().equals("PREVIOUS"))
			task.changeTime(1);
		else if(e.getActionCommand().contains("OPEN1"))
		{
			int temp = Integer.parseInt(e.getActionCommand().replace("OPEN1_", ""));
			for(int i=0;i<actual_events.size();i++)
			{
				System.out.println(i);
				if(actual_events.get(i).getIdEvent()==temp)
				{
					actualEvent = actual_events.get(i);
					fr = new AddFrame(actual_events.get(i).getType(), actual_events.get(i),this);
				}		
			}
		}
		else if(e.getActionCommand().contains("OPEN_"))
		{
			String [] index = e.getActionCommand().split("_");
			System.out.println(e.getActionCommand());
			detailView(Integer.parseInt(index[1]), Integer.parseInt(index[2]), Integer.parseInt(index[3]), Integer.parseInt(index[4]));
		}	
		else if(e.getActionCommand().contains("MENU_"))
		{			
			int temp = Integer.parseInt(e.getActionCommand().replace("MENU_", ""));
			status_view[temp] = !status_view[temp];
			updateMenuA();
			
			task.changeStatusCalendar(temp);
		}
		
		for(int i=0; i<arrange.size();i++)
		{
			if(arrange.elementAt(i).getActionCommand().equals(e.getActionCommand()))
			{
				refreshContent(i);
				task.changeView(i);
				
				//metodo que se mandara llamar desde el de tasker
				//loadEvents(task.getEvents());
			}
		}
		
		for(int j=0; j<options.size();j++)
		{			
			if(options.elementAt(j).getActionCommand().equals(e.getActionCommand()))
			{
				settingSelection=j;
				loadOptionView(j);
				updateMenuB();
			}
		}
		
		for(int k=0;k<3;k++)
		{
			if(e.getActionCommand().equals("ADD_"+k))
			{
				actualEvent= task.createEvent(k);
				AddFrame window = new AddFrame(k,actualEvent,this);
			}
		}
		
		if(e.getActionCommand().equals("DISCARD"))
		{
			if(nuevo)
				task.deleteObject(actualEvent);			
		}
		else if(e.getActionCommand().equals("SAVE"))
		{
			task.saveObject(actualEvent);		
		}
		else if(e.getActionCommand().equals("SALIR"))
		{
			fram.dispose();
		}
		else if(e.getActionCommand().equals("AUTOSTART"))
		{
			stadoauto=!stadoauto;
			if(stadoauto)
				auto.setIcon(new ImageIcon(dir+"select.png"));
			else
				auto.setIcon(new ImageIcon(dir+"noselect.png"));
			
			auto.updateUI();
		}
		for(int ki=0;ki<4;ki++)
		{
			if(e.getActionCommand().contains("R_"))
			{
				String temp = e.getActionCommand().replace("R_", "");
				recurrenceselection= Integer.parseInt(temp);
				updateRecurrence();
			}
		}
		if (e.getActionCommand().equals("SAVE2"))
		{
			if(indexsettings==1)
			{
				task.setAutoStart(stadoauto);
				task.setRecurrentOption(recurrenceselection);
				if(recurrenceselection==3)
					task.setMinutesRefresh(Integer.parseInt(numMinutes.getText()));
			}
			else 
			task.setLink(input.getText());
		}
		else if(e.getActionCommand().equals("REFRESH"))
			task.refresh();
		else if(e.getActionCommand().equals("CANCEL"))
		{
			ActionEvent a = new ActionEvent(new JButton(),0,"ALL");
			genframe.actionPerformed(a);
		}
		
	}
	
	private void updateRecurrence() 
	{
		for(int i=0; i<recurrence.size();i++)
		{
			if(recurrenceselection==i)
				recurrence.elementAt(i).setIcon(new ImageIcon(dir+"select.png"));
			else
				recurrence.elementAt(i).setIcon(new ImageIcon(dir+"noselect.png"));
			recurrence.elementAt(i).updateUI();
			
		}
		
	}

	public void detailView(int week, int day, int hour, int catego )
	{
		fram = new JFrame();
		fram.setSize(new Dimension(200,400));
		fram.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		JLabel lb = new JLabel();
		lb.setLayout(new FlowLayout(FlowLayout.CENTER));
		
		scroll2 = new JScrollPane();
		scroll2.setPreferredSize(new Dimension(180,300));
		scroll2.setBorder(BorderFactory.createLineBorder(Color.white));
		int sum=0;
		
		for(int i=0;i<events_arrange.get(week).get(day).get(hour).get(catego).size();i++)
		{
			JButton bt = new JButton();
			bt.setBackground(azul);
			bt.setForeground(Color.white);
			bt.setText("<html>"+events_arrange.get(week).get(day).get(hour).get(catego).get(i).getSubject()+"</html>");
			bt.setPreferredSize(new Dimension(160,50));
			
			bt.addActionListener(this);
			bt.setActionCommand("OPEN1_"+events_arrange.get(week).get(day).get(hour).get(catego).get(i).getIdEvent());
					
			sum+=55;
			lb.add(bt);
		}
		
		JButton salir = new JButton();
		salir.setContentAreaFilled(false);
		salir.setBorderPainted(false);
		salir.setIcon(new ImageIcon(dir+"Rcancel.png"));
		salir.setPreferredSize(new Dimension(120,40));
		salir.addActionListener(this);
		salir.setActionCommand("SALIR");

		lb.setPreferredSize(new Dimension(160,sum));
		scroll2.getViewport().add(lb);
		
		fram.add(scroll2);
		fram.add(salir);
		fram.setVisible(true);
	}
	
	public void chargeEvent(GenericEvent event)
	{
		actualEvent = event;		
	}
	
	

}
